; TV Noise 16:9 for the Commodore 64 DTV
; Compiled with 64TASS

; Original by HMVDVA/HeMa!
; sized down by Peiselulli

; Assembled with ACME
;

        !to "tvnoise.prg",cbm
        !cpu 6510

noisebank	= $4000

;------------------------------------------
; macros
;------------------------------------------

!MACRO sac .v {
    !BYTE $32,.v
}

;------------------------------------------

!MACRO sir .v {
    !byte $42,.v
}

;------------------------------------------

!MACRO bra .v {
    !byte $12, (.v - 2) - *
}

*= $0801
        !byte $0c,$08,$0a,$00,$9e,$32,$30,$36,$31,$00,$00,$00

start:
			sei
                        lda #$35
                        sta $01
			+sir $92	;enable burstmode and skipcycles
			ldy #%00000011
			+sir $a2	;
                        ldy #$d0

                        lda #$01
			sta <$D03F	;Enable extended features

                        ldx #$0f
-			TXA	
			sta $D200,X
			DEX
			bne	-
                        +sac $dd
			lda #($10000/$4000)
			+sac $00

pageloop:
                        ldy #>noisebank

-

			adc $d012 ; random
			eor $dc04 ; generator deluxe... ahum
			
			and #$0f
			sta 00,x

                        inx
			bne -

                        iny
                        bpl -
                        
                        +sir $ad
                        inx
                        cpx #$0a
                        +sir $a2
                        bne pageloop
                        ldy #$d0
                        
                        
                        ldx #$00
			stx <$D049	;Screen location LOW
			stx <$D04A	;Screen location MID
			stx <$d020
			stx <$d021
                        stx $07f8
                        stx $07f9
                        stx $07fa
                        stx $07fb

			lda #$55
			sta <$D03C	;Enable Linear Mode
			lda #$5B
			sta <$D011	
			lda #$1f
			sta <$D016
			lda #$08
			sta <$D04C	;Stepping=8
			lda #$41
			sta <$D04B	;Screen location HIGH

                        lda #$32
                        sta <$d012
                        lda #<irq
                        sta $fffe
                        lda #>irq
                        sta $ffff
                        lda #$81
                        sta <$d019
                        sta <$d01a
                        lda #$3f
                        sta <$D04D
                        lda #$1f
                        sta <$d044
                        lda #$81
                        sta <$d01a
                        lda #$7f
                        sta $dc0d
                        lda $dc0d
                        cli

loop
                        adc <$d012 ; random
			eor $dc04 ; generator deluxe... ahum
                        eor $d044
                        tax
                        adc <$d012 ; random
			eor $dc04 ; generator deluxe... ahum
                        sta $d400,x                      
                        
			+bra loop


        * = (*+3) & 0xfffc
        
irq:    nop
        sta irq_end+1
        +sir $10
        lda #$f8
        nop
        ora $d044
-       nop
        inx
        bne -
        nop
        nop
        lda #$8f
        nop
        sta $d019
        nop
        inc $d012
        nop
        nop
        nop
        nop
        nop
        nop
        lda #%01111101
        nop
        sta $d03c
irq_counter:
        lda #201
        beq +
        nop
        lda d011_table,x
        nop
        sta $d011
        nop
        dec irq_counter+1
        
        !for i,4 {
        nop
        nop
        nop
        nop
        }
        nop
        nop
        +sir $12
        lda #%01111111
        nop
        sta $d03c
irq_end:
        lda #$00
        rti

+       lda #$32
        sta $d012
        lda #201
        sta irq_counter+1
        lda #%01111101		;set linear mode

	sta $d03c
        +sir $12
	lda $dc04	;
	sta $D049	;Screen location LOW
	lda $dc05	;
	sta $D04A	;Screen location MID
        +bra irq_end        

d011_table:
        !byte 0
        !for i,201 {
        !byte $5f - ((i+3) & $07)
        }

